package com.whlilong.jxc.dialog;

import java.math.BigDecimal;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.whlilong.jxc.model.Depart;
import com.whlilong.jxc.model.Yginfo;
import com.whlilong.jxc.sys.ExtentionInfo;
import org.eclipse.swt.widgets.Combo;
import com.swtdesigner.ResourceManager;

public class StaffInfoCreate extends AbstractDialog {

	private List<Yginfo> list = ExtentionInfo.getInstance().getStaffInfoList();
	private TableViewer tableViewer;
	private Text textStaffid;
	private Text textStaffname;
	private Text textXb;
	private Text textStatus;
	private Text textCardid;
	private Text textTag;
	private Text textJob;
	private Text textData;
	private Text textXlname;
	private Text textBirth;
	private Text textTel;
	private Text textAddress;
	private Text textJbgz;
	private Combo comboPart;

	public StaffInfoCreate(Shell parentshell, TableViewer tableViewer) {
		// TODO Auto-generated constructor stub
		super(parentshell);
		this.tableViewer = tableViewer;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("新建数据");
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, Add_id, "继续添加(&A)", false);
		createButton(parent, IDialogConstants.OK_ID, "添加(&S)", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消(&X)", false);

	}

	public Control createGroupText(Composite parent) {
		FormLayout formLayout = (FormLayout) parent.getLayout();
		formLayout.marginBottom = 20;
		formLayout.marginTop = 20;

		CTabFolder tabFolder = new CTabFolder(parent, SWT.BORDER);
		FormData fd_tabFolder = new FormData();
		fd_tabFolder.right = new FormAttachment(100);
		fd_tabFolder.left = new FormAttachment(5);
		tabFolder.setLayoutData(fd_tabFolder);
		tabFolder.setSimple(false);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
		tabItem.setImage(ResourceManager.getPluginImage("com.whlilong.jxc", "icons/brand1.gif"));
		tabItem.setText("基本信息");

		Composite composite = new Composite(tabFolder, SWT.BORDER);
		tabItem.setControl(composite);
		GridLayout gl_composite = new GridLayout(6, false);
		gl_composite.marginRight = 10;
		gl_composite.marginLeft = 10;
		gl_composite.marginTop = 10;
		gl_composite.verticalSpacing = 10;
		gl_composite.marginBottom = 20;
		composite.setLayout(gl_composite);

		Label labelStaffid = new Label(composite, SWT.NONE);
		labelStaffid.setBounds(0, 0, 60, 17);
		labelStaffid.setText("员工编号：");

		textStaffid = new Text(composite, SWT.BORDER);
		GridData gd_textStaffid = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_textStaffid.widthHint = 100;
		textStaffid.setLayoutData(gd_textStaffid);
		textStaffid.setBounds(0, 0, 73, 23);
		
		Label labelXb = new Label(composite, SWT.NONE);
		labelXb.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		labelXb.setText("性别：");
		
		textXb = new Text(composite, SWT.BORDER);
		GridData gd_textXb = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_textXb.widthHint = 100;
		textXb.setLayoutData(gd_textXb);
		
		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("状态：");
		
		textStatus = new Text(composite, SWT.BORDER);
		GridData gd_textStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_textStatus.widthHint = 100;
		textStatus.setLayoutData(gd_textStatus);

		Label labelStaffname = new Label(composite, SWT.NONE);
		labelStaffname.setBounds(0, 0, 61, 17);
		labelStaffname.setText("员工姓名：");

		textStaffname = new Text(composite, SWT.BORDER);
		GridData gd_textStaffname = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_textStaffname.widthHint = 100;
		textStaffname.setLayoutData(gd_textStaffname);
		textStaffname.setBounds(0, 0, 73, 23);
		
		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("身份证号：");
		
		textCardid = new Text(composite, SWT.BORDER);
		textCardid.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("所属部门：");
		
		comboPart = new Combo(composite, SWT.READ_ONLY);
		comboPart.add("不定");
		List departList = ExtentionInfo.getInstance().getDepartInfoList();
		for (int i = 0; i < departList.size(); i++) {
			Depart ks = (Depart) departList.get(i);
			comboPart.add(ks.getDepartname());
		}
		comboPart.select(0);
		comboPart.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_4 = new Label(composite, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText("员工权限：");
		
		textTag = new Text(composite, SWT.BORDER);
		textTag.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_5 = new Label(composite, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_5.setText("职位：");
		
		textJob = new Text(composite, SWT.BORDER);
		textJob.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_6 = new Label(composite, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_6.setText("入职时间：");
		
		textData = new Text(composite, SWT.BORDER);
		textData.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_7 = new Label(composite, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_7.setText("学历：");
		
		textXlname = new Text(composite, SWT.BORDER);
		textXlname.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_8 = new Label(composite, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setText("生日：");
		
		textBirth = new Text(composite, SWT.BORDER);
		textBirth.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_9 = new Label(composite, SWT.NONE);
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_9.setText("联系电话：");
		
		textTel = new Text(composite, SWT.BORDER);
		textTel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_10 = new Label(composite, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_10.setText("住址：");
		
		textAddress = new Text(composite, SWT.BORDER);
		textAddress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_11 = new Label(composite, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_11.setText("基本工资：");
		
		textJbgz = new Text(composite, SWT.BORDER);
		textJbgz.setText("0");
		textJbgz.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);

		CTabItem tabItem_1 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("辅助信息");

		return parent;

	}

	protected void continueAdd() {
		if (!textStaffname.getText().equals("")
				&& textStaffid.getText() != null
				&& !textStaffname.getText().equals("")
				&& textStaffname.getText() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (textStaffid.getText().equals(list.get(i).getYgid())) {
					MessageDialog.openError(getShell(), "错误", "编号重复，添加失败！");
					return;
				}
			}
			exeSearch();
			initial();
			tableViewer.setInput(list);
			tableViewer.getTable().select(list.size() - 1);

		} else {
			MessageDialog.openError(getShell(), "错误", "编号或名称为空，添加失败！");
		}

	}

	private void initial() {
		textStaffid.setText("");
		textStaffname.setText("");
		textXb.setText("");
		textStatus.setText("");
		textCardid.setText("");
		textTag.setText("");
		textJob.setText("");
		textData.setText("");
		textXlname.setText("");
		textBirth.setText("");
		textTel.setText("");
		textAddress.setText("");
		textJbgz.setText("0");
		
		comboPart.select(0);

	}

	protected void exeSearch() {
		if (!textStaffname.getText().equals("")
				&& textStaffid.getText() != null
				&& !textStaffname.getText().equals("")
				&& textStaffname.getText() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (textStaffid.getText().equals(list.get(i).getYgid())) {
					MessageDialog.openError(getShell(), "错误", "编号重复，添加失败！");
					return;
				}
			}
			Yginfo staff = new Yginfo();
			if (list.size() > 0)
				staff.setId(list.get(list.size() - 1).getId() + 1);
				
			else
				staff.setId(1);
			staff.setYgid(textStaffid.getText());
			staff.setYgname(textStaffname.getText());
			staff.setPartname(comboPart.getText());
			staff.setXb(textXb.getText());
			staff.setStatus(textStatus.getText());
			staff.setAddress(textAddress.getText());
			staff.setBirthday(textBirth.getText());
			staff.setCardid(textCardid.getText());
			staff.setDate(textData.getText());
			staff.setJdgz(BigDecimal.valueOf(Double.valueOf(textJbgz.getText())));
			staff.setTel(textTel.getText());
			staff.setXlname(textXlname.getText());
			staff.setZwname(textJob.getText());
			staff.setTag(textTag.getText());

			list.add(staff);
			isAdd = true;
		} else {
			MessageDialog.openError(getShell(), "错误", "编号或名称为空，添加失败！");
			isAdd = false;
		}
	}

	protected void okPressed() {
		if (isAdd) {
			setReturnCode(OK);
			close();
		}
	}

	public boolean isAdd() {
		return isAdd;
	}

	public void setAdd(boolean isAdd) {
		this.isAdd = isAdd;
	}

	public List<Yginfo> getList() {
		// TODO Auto-generated method stub
		return list;
	}

	public void setList(List<Yginfo> list) {
		this.list = list;
	}
}
